//自定义js
//检测手机号是否正确  

//公共配置
layer.config({
    extend: ['extend/layer.ext.js', 'skin/moon/style.css'],
    skin: 'layer-ext-moon'
});

// 表单验证错误图标
var icon = "<i class='fa fa-times-circle'></i> ";

/**
 * easyui treegrid 无数据时显示信息
 */
var myview = $.extend({},$.fn.treegrid.defaults.view,{
    onAfterRender:function(target){
        $.fn.treegrid.defaults.view.onAfterRender.call(this,target);
        var opts = $(target).treegrid('options');
        var vc = $(target).treegrid('getPanel').children('div.datagrid-view');
        vc.children('div.datagrid-empty').remove();
        if (!$(target).treegrid("getData").length){
            var d = $('<div class="datagrid-empty"></div>').html(opts.emptyMsg || '没有找到匹配的记录').appendTo(vc);
            d.css({
                position:'absolute',
                left:0,
                top:50,
                width:'100%',
                textAlign:'center'
            });
        }
    }
});

$(document).ready(function () {
	// tooltips
	$('[data-toggle=tooltip]').tooltip({
	    container: "body"
	});
	
	$("[data-toggle=popover]").popover();
	
	// toastr默认设置
	toastr.options = {
	  "closeButton": false,
	  "debug": true,
	  "progressBar": true,
	  "positionClass": "toast-top-center",
	  "showDuration": "400",
	  "hideDuration": "1000",
	  "timeOut": "3000",
	  "extendedTimeOut": "1000",
	  "showEasing": "swing",
	  "hideEasing": "linear",
	  "showMethod": "fadeIn",
	  "hideMethod": "fadeOut"
	}
	
	// ---------------------jquery-validate---------------------------//
	// 手机号码验证
	$.validator.addMethod("mobile", function(value, element) {  
	    var length = value.length;  
	    var regPhone = /^[0-9]{11}$/;  
	    return this.optional(element) || ( length == 11 && regPhone.test( value ) );    
	}, "请输入正确的手机号码");
	
	// 连接验证
	$.validator.addMethod("url", function(value, element) {  
	    var urlReg = "^((https|http)?://)";  
	    return this.optional(element) || ( new RegExp(urlReg).test( value ) );    
	}, "请输入正确的手机号码");
	
	// 正整数验证
	$.validator.addMethod("positiveinteger", function(value, element) {  
		 var aint = parseInt(value);	
		 return aint >= 0 && (aint + "") == value;   
	}, "请输入大于0的正整数");   
	// ---------------------jquery-validate---------------------------//
	
	
    // 以下为修改jQuery Validation插件兼容Bootstrap的方法，没有直接写在插件中是为了便于插件升级
	// help-block m-b-none
    $.validator.setDefaults({
        highlight: function (element) {
            $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
        },
        success: function (element) {
            element.closest('.form-group').removeClass('has-error').addClass('has-success');
        },
        errorElement: "span",
        errorPlacement: function (error, element) {
            if (element.is(":radio") || element.is(":checkbox")) {
                error.appendTo(element.parent().parent().parent());
            } else {
                error.appendTo(element.parent());
            }
        },
        errorClass: "help-block m-b-none",
        validClass: "help-block m-b-none"
    });
	
    // 解决jquery.validate和chosen在下拉框验证时的冲突
	// $.validator.setDefaults({ ignore: ":hidden:not(select)" });
	
    // MetsiMenu
    $('#side-menu').metisMenu();
    
    // 添加返回顶部按钮
    $("<div class='back2top'></div>").appendTo("body");
    
    // 监听scoll事件
    $(window).scroll(function(){
		$(this).scrollTop() > 10 ? $(".back2top").addClass("scrolled") : $(".back2top").removeClass("scrolled")
	})
    
    // 回到顶部
    $('.back2top').click(function(e) {
    	$("html,body").animate({
			scrollTop: 0
		}, "slow");
		e.preventDefault();
	});
    
    //折叠ibox
    $('.collapse-link').click(function () {
        var ibox = $(this).closest('div.ibox');
        var button = $(this).find('i');
        var content = ibox.find('div.ibox-content');
        content.slideToggle(200);
        button.toggleClass('fa-chevron-up').toggleClass('fa-chevron-down');
        ibox.toggleClass('').toggleClass('border-bottom');
        setTimeout(function () {
            ibox.resize();
            ibox.find('[id^=map-]').resize();
        }, 50);
    });

    //关闭ibox
    $('.close-link').click(function () {
        var content = $(this).closest('div.ibox');
        content.remove();
    });

    // 打开右侧边栏
    $('.right-sidebar-toggle').click(function () {
        $('#right-sidebar').toggleClass('sidebar-open');
    });

    // 右侧边栏使用slimscroll
    $('.sidebar-container').slimScroll({
        height: '100%',
        railOpacity: 0.4,
        wheelStep: 10
    });

    // 打开聊天窗口
    $('.open-small-chat').click(function () {
        $(this).children().toggleClass('fa-comments').toggleClass('fa-remove');
        $('.small-chat-box').toggleClass('active');
    });

    // 聊天窗口使用slimscroll
    $('.small-chat-box .content').slimScroll({
        height: '234px',
        railOpacity: 0.4
    });

    // Small todo handler
    $('.check-link').click(function () {
        var button = $(this).find('i');
        var label = $(this).next('span');
        button.toggleClass('fa-check-square').toggleClass('fa-square-o');
        label.toggleClass('todo-completed');
        return false;
    });

    //固定菜单栏
    $(function () {
        $('.sidebar-collapse').slimScroll({
            height: '100%',
            railOpacity: 0.9,
            alwaysVisible: false
        });
    });   
    

    // 菜单切换
    $('.navbar-minimalize').click(function () {
        $("body").toggleClass("mini-navbar");
        SmoothlyMenu();
    });


    // 侧边栏高度
    function fix_height() {
        var heightWithoutNavbar = $("body > #wrapper").height() - 61;
        $(".sidebard-panel").css("min-height", heightWithoutNavbar + "px");
    }
    fix_height();

    $(window).bind("load resize click scroll", function () {
        if (!$("body").hasClass('body-small')) {
            fix_height();
        }
    });

    //侧边栏滚动
    $(window).scroll(function () {
        if ($(window).scrollTop() > 0 && !$('body').hasClass('fixed-nav')) {
            $('#right-sidebar').addClass('sidebar-top');
        } else {
            $('#right-sidebar').removeClass('sidebar-top');
        }
    });

    $('.full-height-scroll').slimScroll({
        height: '100%'
    });

    $('#side-menu>li').click(function () {
        if ($('body').hasClass('mini-navbar')) {
            NavToggle();
        }
    });
    $('#side-menu>li li a').click(function(){
        if ($(window).width() < 769) {
            NavToggle();            
        }
    });
    
    $('.nav-close').click(NavToggle);

});

$(window).bind("load resize", function () {
    if ($(this).width() < 769) {
        $('body').addClass('mini-navbar');
        $('.navbar-static-side').fadeIn();
    }
});

function NavToggle(){
    $('.navbar-minimalize').trigger('click');
}

function SmoothlyMenu() {
    if (!$('body').hasClass('mini-navbar')) {
        $('#side-menu').hide();
        setTimeout(
            function () {
                $('#side-menu').fadeIn(500);
            }, 100);
    } else if ($('body').hasClass('fixed-sidebar')) {
        $('#side-menu').hide();
        setTimeout(
            function () {
                $('#side-menu').fadeIn(500);
            }, 300);
    } else {
        $('#side-menu').removeAttr('style');
    }
}


//主题设置    
$(function () {
    
    $.get("skin-config.html", function (data) {
        $('body').append(data);
    });
    
    if (localStorageSupport) {

        var collapse = localStorage.getItem("collapse_menu");
        var fixednavbar = localStorage.getItem("fixednavbar");
        var boxedlayout = localStorage.getItem("boxedlayout");

        var body = $('body');

        if (collapse == 'on') {
            if (!body.hasClass('body-small')) {
                body.addClass('mini-navbar');
            }
        }

        if (fixednavbar == 'on') {
            $(".navbar-static-top").removeClass('navbar-static-top').addClass('navbar-fixed-top');
            body.addClass('fixed-nav');
        }

        if (boxedlayout == 'on') {
            body.addClass('boxed-layout');
        }
    }
});

/**
 * 封装jquery.ajax方法
 * @param url
 * @param postData
 * @param successFun
 * @param errorFun
 */
function ajaxJson(url, postData, successFun, errorFun) {
    $.ajax({
        url: url,
        type: "post",
        data: postData,
        dataType: "json",
        async: false,
        success: function (data) {
        	if ($.isFunction(successFun))
        		successFun(data);
        },
        error: function (data) {
        	if ($.isFunction(errorFun))
        		errorFun(data);
        }
    });
}

/**
 * 封装jquery.ajax方法
 * @param url
 * @param postData
 * @param successFun
 * @param errorFun
 */
function ajaxText(url, postData, successFun, errorFun) {
    $.ajax({
    	url: url,
        type: 'post',
        dataType: "text",
        data: postData ,
        cache: false,
        async: false,
        success: function (data) {
        	if ($.isFunction(successFun))
        		successFun(data);
        },
        error: function (data) {
        	if ($.isFunction(errorFun))
        		errorFun(data);
        }
    });
}

// 判断浏览器是否支持html5本地存储
function localStorageSupport() {
    return (('localStorage' in window) && window['localStorage'] !== null)
}


